//
//  ViewController.m
//  PointConverter
//
//  Created by qinjilei on 2018/11/6.
//  Copyright © 2018年 qinjilei. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "PositionUtil.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self openMap];
}

-(void)openMap{
    //获取当前位置
    MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
    //当前经维度
    double currentLat=mylocation.placemark.location.coordinate.latitude;
    double currentLon=mylocation.placemark.location.coordinate.longitude;
    
    
    //目的地位置116.389034,39.933748默认bd09坐标系
  
    NSString *address= @"这是一个地址";
    double shopLat=39.933748;
    double shopLon=116.389034;
    
    
    CLLocationCoordinate2D toLocationCoordinate = CLLocationCoordinate2DMake(shopLat,shopLon);
    
    if([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        toLocationCoordinate= [PositionUtil bd09_To_Gps84:shopLat withLon:shopLon];
        NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",appName, @"hsq", address, toLocationCoordinate.latitude, toLocationCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    }else  if([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
        
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=driving",currentLat, currentLon,toLocationCoordinate.latitude,toLocationCoordinate.longitude,address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    } else if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]){
        toLocationCoordinate= [PositionUtil bd09_To_Gcj02:shopLat withLon:shopLon];
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:toLocationCoordinate addressDictionary:nil]];
        [toLocation setName:address];
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        
    }else{
        NSLog(@"未安装任何地图,请安装最新版高德或者百度地图");
    }
    
}


@end
